package com.app.micaihu.view.main.game.presenter

import com.app.base.mvp.BasePresenter
import com.app.http.NetRequestListener
import com.app.http.bean.BaseBean
import com.app.micaihu.bean.game.GameCenterEntity
import com.app.micaihu.bean.game.GameContent
import com.app.micaihu.bean.game.GameDownloadEntity
import com.app.micaihu.configure.LoadType
import com.app.micaihu.utils.ApiServiceUtils.apiService
import com.app.micaihu.utils.ParamsMapUtils
import com.app.micaihu.view.army.contract.ArmyCreateContract
import com.app.micaihu.view.bean.ArmyCreateInfo
import com.app.micaihu.view.main.game.contract.GameClassListContract
import com.app.micaihu.view.main.game.contract.GameContract
import com.app.utils.biz.Biz

/**
 * 游戏 class
 */
open class GameClassListPresenter : BasePresenter<GameClassListContract.View>(), GameClassListContract.Presenter {

    private var isRefresh = true
    private var page = 1
    private var type = 0
    override fun gameClassList(type: Int) {
        this.type = type
        val params = ParamsMapUtils.commonParams
        params["page"] = page
        params["type"] = type
        requestNetwork(apiService.gameClassList(params), object : NetRequestListener<BaseBean<List<GameContent>?>?> {
            override fun onSuccess(result: BaseBean<List<GameContent>?>?) {
                page++
                getView()?.hideLoading()
                val loadType = if (isRefresh) LoadType.TYPE_REFRESH_SUCCESS else LoadType.TYPE_LOAD_MORE_SUCCESS
                getView()?.handleGameClassList(result, loadType)
            }

            override fun onFailed(e: Throwable?) {
                getView()?.hideLoading()
                val loadType = if (isRefresh) LoadType.TYPE_REFRESH_ERROR else LoadType.TYPE_LOAD_MORE_ERROR
                getView()?.handleGameClassList(null, loadType)
                getView()?.onError(e)
            }
        })
    }

    override fun refresh() {
        page = 1
        isRefresh = true
        gameClassList(type)
    }

    override fun loadMore() {
        isRefresh = false
        gameClassList(type)
    }

}